package com.kakarote.examine.controller;


import cn.hutool.core.util.ObjectUtil;
import com.kakarote.core.common.ApiExplain;
import com.kakarote.core.common.ParamAspect;
import com.kakarote.core.common.Result;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.admin.entity.AdminCompany;
import com.kakarote.core.feign.examine.entity.*;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.examine.constant.ExamineFlowFlagEnum;
import com.kakarote.examine.entity.BO.ExamineAdminBO;
import com.kakarote.examine.entity.BO.ExamineBO;
import com.kakarote.examine.entity.BO.ExaminePreviewBO;
import com.kakarote.examine.entity.PO.ExamineRecordLog;
import com.kakarote.examine.entity.VO.ExamineRecordCustomerVO;
import com.kakarote.examine.entity.VO.ExamineRecordLogVO;
import com.kakarote.examine.entity.VO.ExamineRecordVO;
import com.kakarote.examine.entity.VO.ExamineReturnFlowVO;
import com.kakarote.examine.service.IExamineRecordLimitService;
import com.kakarote.examine.service.IExamineRecordLogService;
import com.kakarote.examine.service.IExamineRecordService;
import com.kakarote.examine.service.IExamineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审核记录表 前端控制器
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-11-19
 */
@RestController
@RequestMapping("/examineRecord")
@Api(tags = "审批记录接口")
public class ExamineRecordController {

    @Autowired
    private IExamineRecordService examineRecordService;

    @Autowired
    private IExamineRecordLogService examineRecordLogService;

    @Autowired
    private IExamineService examineService;

    @Autowired
    private IExamineRecordLimitService examineRecordLimitService;

    @PostMapping("/addExamineRecord")
    @ApiExplain("添加审批记录")
    public Result<ExamineRecordReturnVO> addExamineRecord(@RequestBody ExamineRecordSaveBO examineRecordSaveBO) {
        ExamineRecordReturnVO examineRecordVO = examineRecordService.addExamineRecord(examineRecordSaveBO);
        return Result.ok(examineRecordVO);
    }


    @PostMapping("/addExamineFlowFinalNode")
    @ApiOperation("添加审批节点")
    public Result<ExamineInfoVo> addExamineFlowFinalNode(@RequestBody ExamineFlowFinalBO examineFlowFinalBO) {
        examineService.addExamineFlowFinalNode(examineFlowFinalBO, ExamineFlowFlagEnum.APPLY.getType());
        return Result.ok();
    }

    @PostMapping("/forwardOthers")
    @ApiOperation("转他人处理")
    public Result<ExamineInfoVo> forwardOthers(@RequestBody ExamineForwardOthersBO examineForwardOthersBO) {
        examineRecordService.forwardOthers(examineForwardOthersBO);
        return Result.ok();
    }

    @PostMapping("/solicitOthers")
    @ApiOperation("征求他人意见")
    public Result<ExamineInfoVo> solicitOthers(@RequestBody ExamineFlowFinalBO examineFlowFinalBO) {
        examineRecordService.solicitOthers(examineFlowFinalBO);
        return Result.ok();
    }


    @PostMapping("/queryAuthField")
    @ApiOperation("查询权限字段")
    public Result<List<?>> queryAuthField(@RequestBody ExamineBO examineBO) {
        return Result.ok(examineRecordService.queryAuthField(examineBO));
    }


    @PostMapping("/auditExamine")
    @ApiOperation("进行审批")
    public Result auditExamine(@RequestBody ExamineBO examineBO) {
        examineRecordService.auditExamine(examineBO);
        return Result.ok();
    }


    @PostMapping("queryExamineUpdateLog")
    @ApiOperation("查询审批修改记录")
    public Result<List<Map<String, Object>>> queryExamineUpdateLog(@RequestBody ExamineBO examineBO) {
        return Result.ok(examineRecordService.queryExamineUpdateLog(examineBO));
    }


    @PostMapping("/batchAuditExamine")
    @ApiOperation("进行审批(待办批量)")
    public Result batchAuditExamine(@RequestBody ExamineBO examineBO) {
        // pc
        if (ObjectUtil.isNull(examineBO.getType())) {
            List<ExamineBO> examineBOS = examineBO.getExamineBOS();
            if (ObjectUtil.length(examineBOS) == 0) {
                throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
            }
            for (ExamineBO bo : examineBOS) {
                examineRecordService.auditExamine(bo);
            }
        }
        // app
        else {
            examineService.batchAuditExamine(examineBO);
        }
        return Result.ok();
    }

    @PostMapping("/queryExamineRecordLog")
    @ApiOperation("获取完整审批历史记录")
    public Result<List<ExamineRecordLogVO>> queryExamineRecord(@RequestParam(value = "ownerUserId", required = false) String ownerUserId, @RequestParam("recordId") Long recordId, @RequestParam(value = "typeId") Long typeId) {
        return Result.ok(examineRecordLogService.queryExamineRecordLog(recordId, ownerUserId, typeId));
    }

    @PostMapping("/queryExamineRecordInfo")
    @ApiOperation("获取未结束审批流程的待审核人")
    public Result<ExamineRecordReturnVO> queryExamineRecordInfo(@RequestParam("recordId") Long recordId) {
        return Result.ok(examineRecordService.queryExamineRecordInfo(recordId));
    }


    @PostMapping("/queryExamineLogById")
    @ApiExplain("获取指定审批历史记录")
    public Result<ExamineRecordLog> queryExamineLogById(@RequestParam("examineLogId") Integer examineLogId) {
        return Result.ok(examineRecordLogService.getById(examineLogId));
    }

    @PostMapping("/deleteExamineRecord")
    @ApiExplain("同步删除审批数据")
    public Result<Boolean> deleteExamineRecord(@RequestParam("recordId") Long recordId) {
        return Result.ok(examineRecordService.deleteExamineRecord(recordId));
    }

    @PostMapping("/updateExamineRecord")
    @ApiExplain("同步修改审批状态")
    public Result<Boolean> updateExamineRecord(@RequestParam("recordId") Long recordId, @RequestParam("examineStatus") Integer examineStatus) {
        return Result.ok(examineRecordService.updateExamineRecord(recordId, examineStatus));
    }

    @PostMapping("/deleteExamineRecordAndLog")
    @ApiExplain("初始化用删除审批历史数据")
    public Result<Boolean> deleteExamineRecordAndLog(@RequestParam("label") Integer label) {
        return Result.ok(examineRecordService.deleteExamineRecordAndLog(label));
    }

    @PostMapping("/queryExamineRecordList")
    @ApiOperation("获取审批详情")
    public Result<ExamineRecordVO> queryExamineRecordList(@RequestParam("recordId") Long recordId, @RequestParam(value = "ownerUserId", required = false) Long ownerUserId) {
        return Result.ok(examineRecordService.queryExamineRecordList(recordId, ownerUserId, null, 1));
    }

    @PostMapping("/timeOutRecordHandlerParam")
    @ApiOperation("查询超时未处理recordList")
    public Result<List<Long>> timeOutRecordHandlerParam(@RequestBody List<Long> recordIdList) {
        examineRecordService.autoHandle(recordIdList);
        return Result.ok(recordIdList);
    }

    @PostMapping("/queryExamineRecordListByTypeId")
    @ApiOperation("获取审批详情")
    public Result<List<ExamineRecordCustomerVO>> queryExamineRecordListByTypeId(@RequestParam("typeId") Long typeId, @RequestParam(value = "ownerUserId", required = false) Long ownerUserId) {
        return Result.ok(examineRecordService.queryExamineRecordListByTypeId(typeId, ownerUserId));
    }

    @PostMapping("/timeOutRecordHandler")
    @ParamAspect
    @ApiOperation("查询超时未处理recordList")
    public Result<List<Long>> timeOutRecordHandler(@RequestParam("userId") Long userId) {
        UserUtil.setUser(userId);
        //删除过期email 对外token
        examineRecordService.removeInvalidEmailToken();
        //审批限时处理
        examineRecordService.autoHandle(new ArrayList<>());
        return Result.ok();
    }






    @PostMapping("/queryRecordLog")
    @ApiOperation("查询审批人拒绝(管理员恢复)审批返回节点")
    public Result<List<ExamineReturnFlowVO>> queryRecordLog(@RequestParam Long recordId) {
        return Result.ok(examineRecordLogService.queryRecordFlowLog(recordId, true));
    }

    @PostMapping("/adminAuditExamine")
    @ApiOperation("管理员审批干预（恢复与跳过）")
    public Result adminAuditExamine(@RequestBody ExamineAdminBO examineBO) {
        examineRecordService.adminAuditExamine(examineBO);
        return Result.ok();
    }

    @PostMapping("/adminQueryExamineFlow")
    @ApiOperation("管理员跳过预览审批流程节点")
    public Result<List<ExamineReturnFlowVO>> adminQueryExamineFlow(@RequestBody ExaminePreviewBO examinePreviewBO) {
        List<ExamineReturnFlowVO> examineFlowVO = examineRecordService.adminExamineFlowId(examinePreviewBO);
        return Result.ok(examineFlowVO);
    }


}

